using ShopPowerTemplate.Entities;
using ShopPowerTemplate.WcfService.Auth;
using PaypalService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Channels;
using System.ServiceModel;


namespace ShopPowerTemplate.WcfService
{
    [CustomServiceBehavior]
    public partial class WebAuthService : WebAuthServiceBase, IWebAuthService
    {
        public void AddReview(Review review)
        {
            ShopPowerTemplate.Services.IReviewService service = IoC.IoCFactory.Resolve<ShopPowerTemplate.Services.IReviewService>();
            service.Add(review);
        }

        public string GetPaypalCheckOutUrl(Order o)
        {
            decimal total = o.OrderDetails.Sum(od => 
                od.Price  //Price
                * (1 - Math.Abs((od.Discount ?? 0) / 100)) //Discount
                * (1 + Math.Abs(od.Tax / 100)) //Tax
                * od.Quantity) //Quantity
                + o.ShippingCost; //Shipping cost
            return PaypalPaymentService.Instance.GetPaypalCheckOutUrl(total, o.Id);
        }

        public Address FindAddress(int id)
        {
            ShopPowerTemplate.Services.IAddressService service = IoC.IoCFactory.Resolve<ShopPowerTemplate.Services.IAddressService>();
            return service.Find(id);
        }

        public Address AddAddress(Address address)
        {
            ShopPowerTemplate.Services.IAddressService service = IoC.IoCFactory.Resolve<ShopPowerTemplate.Services.IAddressService>();
            service.Add(address);

            return address;
        }

        public void ChangeAddress(Address address)
        {
            ShopPowerTemplate.Services.IAddressService service = IoC.IoCFactory.Resolve<ShopPowerTemplate.Services.IAddressService>();
            service.Change(address);
        }

        public void RemoveAddress(Address address)
        {
            ShopPowerTemplate.Services.IAddressService service = IoC.IoCFactory.Resolve<ShopPowerTemplate.Services.IAddressService>();
            service.Remove(address);
        }

        public ICollection<Address> GetCustomerAddresses()
        {
            MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;
            string customerEmail = headers.GetHeader<string>("User", "http://www.icinetic.com");

            ShopPowerTemplate.Services.ICustomerService service = IoC.IoCFactory.Resolve<ShopPowerTemplate.Services.ICustomerService>();
            Customer customer = service.Find(customerEmail);

            return customer.Addresses;
        }

        public Order AddOrder(Order order)
        {
            MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;
            string customerEmail = headers.GetHeader<string>("User", "http://www.icinetic.com");

            ShopPowerTemplate.Services.ICustomerService customerService = IoC.IoCFactory.Resolve<ShopPowerTemplate.Services.ICustomerService>();
            Customer customer = customerService.Find(customerEmail);

            order.Customer = customer;
            order.Reference = string.Format("{0}-{1}", customer.Id.ToString(), DateTime.Now.ToString("ddMMyyyyHHmmss"));

            ShopPowerTemplate.Services.IOrderStateService orderStateService = IoC.IoCFactory.Resolve<ShopPowerTemplate.Services.IOrderStateService>();
            order.OrderState = orderStateService.Find((int)OrderStateEnum.NuevoPedido);

            ShopPowerTemplate.Services.IOrderService orderService = IoC.IoCFactory.Resolve<ShopPowerTemplate.Services.IOrderService>();
            orderService.Add(order);
            return order;
        }


        public Order FindOrder(int id)
        {
            ShopPowerTemplate.Services.IOrderService orderService = IoC.IoCFactory.Resolve<ShopPowerTemplate.Services.IOrderService>();
            return orderService.Find(id);
        }
    }
}

